package view.components;

import java.awt.SystemColor;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Date;
import java.util.Locale;

import org.jdesktop.swingx.JXDatePicker;
import org.jdesktop.swingx.JXMonthView;
import org.jdesktop.swingx.plaf.basic.BasicDatePickerUI;

@SuppressWarnings("serial")
public class ExtendedJXDatePicker extends JXDatePicker {
	
	public ExtendedJXDatePicker(){
		this(new Date(), Locale.GERMAN);
	}
	
	public ExtendedJXDatePicker(Locale l){
		this(new Date(), l);
	}
	
	public ExtendedJXDatePicker(Date d){
		this(d, Locale.GERMAN);
	}
	
	public ExtendedJXDatePicker(Date d, Locale l){
		super(d, l);
		
		getEditor().setEditable(false);
		getEditor().setBackground(SystemColor.WHITE);
		getEditor().addMouseListener(new MouseAdapter(){

			@Override
			public void mouseClicked(MouseEvent e) {
				BasicDatePickerUI ui = (BasicDatePickerUI) ExtendedJXDatePicker.this.getUI();
				ui.toggleShowPopup();
			}
			
		});
	}

	@SuppressWarnings("deprecation")
	public static JXMonthView createMonthView(Date lowerBound, Date upperBound) {
		JXMonthView datePickerRange = new JXMonthView();
		datePickerRange.setLowerBound(lowerBound);
		datePickerRange.setUpperBound(upperBound);
		boolean moreThanOneMonth = lowerBound.getMonth() != upperBound
				.getMonth() || lowerBound.getYear() != upperBound.getYear();
		datePickerRange.setTraversable(moreThanOneMonth);
		return datePickerRange;
	}
}
